import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @description:
 * @Author: wuzhenbin
 * @CreateTime: 2025-02-12
 */
public class leetcode_438 {
    public static void main(String[] args) {
        System.out.println(findAnagrams("abab", "ab"));
    }
    public static  List<Integer> findAnagrams(String s, String p) {
//        ArrayList<Character> set = new ArrayList<>();
//        for (char c : p.toCharArray()) {
//            set.add(c);
//        }
        int[] set = new int[26];
        for (int i = 0; i < p.length(); i++) {
            set[p.charAt(i)-'a']++;
        }
        ArrayList<Integer> resList = new ArrayList<>();
        int left =0;
        int[] set1 = new int[26];
        for (int i = left; i < left+p.length(); i++) {
            set1[s.charAt(i)-'a']++;
        }
        left++;
        while(left+p.length()-1<s.length()){
            if(judge(set,set1))resList.add(left);
            set1[s.charAt(left+p.length()-1)-'a']++;
            set1[s.charAt(left)-'a']--;
            left++;
        }
        return resList;
    }

    private static boolean judge(int[] set, int[] set1) {
        if(set.length!=set1.length)return false;
        for (int i = 0; i < 26; i++) {
            if(set[i]!=set1[i])return false;
        }
        return true;
    }


}
